package com.xiaoyu.merchant.ui.activity.wallet;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.model.MyWalletBean;
import com.xiaoyu.merchant.network.NetworkManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class WalletActivity extends BaseActivityWithTitle {

    public static final String BANK_CARD_NAME = "bank_card_name";
    public static final String BANK_CARD_NUM = "bank_card_num";
    private TextView mSet;
    @BindView(R.id.wallet_money)
    TextView mBalance;
    @BindView(R.id.wallet_bank_num)
    TextView mBankNum;
    @BindView(R.id.whether_the_binding_card)
    TextView mWherherBindCard;
    private MyWalletBean bean;

    @Override
    protected void loadContentView(ViewGroup parent) {
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_wallet, parent, true);
        ButterKnife.bind(this);
        setTitleText("我的钱包");
        initView();
    }

    private void initView() {
        mSet = new TextView(this);
        mSet.setText("设置");
        mSet.setTextColor(Color.parseColor("#333333"));
        mSet.setTextSize(18);
        mSet.setOnClickListener(this);
        addRightView(mSet, 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMyWalletInfo();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mSet) {//设置
            startActivity(new Intent(this, SetPwdActivity.class));
        }
    }

    //提现
    @OnClick(R.id.activity_wallet_top_up)
    public void withdrawDepositClick() {
        if (bean.getData().getCardnumber().equals("")) {
            ToastUtil.showToast(WalletActivity.this, "未绑定银行卡不可提现");
        } else {
            Intent intent = new Intent(this, WithdrawDepositActivity.class);
            intent.putExtra(BANK_CARD_NUM, bean.getData().getCardnumber());
            intent.putExtra(BANK_CARD_NAME, bean.getData().getBankname());
            startActivity(intent);
        }

    }

    //明细
    @OnClick(R.id.activity_wallet_details)
    public void detailsClick() {
        startActivity(new Intent(this, ParticularsActivity.class));
    }

    //银行卡
    @OnClick(R.id.activity_wallet_bank_card)
    public void bankCardClick() {
        startActivity(new Intent(this, BankCardActivity.class));
    }

    private void getMyWalletInfo() {
        NetworkManager.getMyWalletInfo(PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                successWallet(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void successWallet(String result) {
        bean = new Gson().fromJson(result, MyWalletBean.class);
        //余额
        if (bean.getData().getBalance().equals("")) {
            mBalance.setText("0.00");
        } else {
            mBalance.setText(bean.getData().getBalance());
        }
        //银行卡号与银行
        mBankNum.setText(bean.getData().getCardnumber() + "    " + bean.getData().getBankname());
        //是否绑定
        if (bean.getData().getCardnumber().equals("")) {
            mWherherBindCard.setText("未绑定银行卡");
        } else {
            mWherherBindCard.setText("");
        }
    }

}